using Pdf2epub.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Xml.Serialization;

namespace Pdf2epub.Model
{
    public class BaseCollection<T> : ObservableCollection<T>, IBulkServiceClient, IDisposable
    {
        private readonly List<NotifyCollectionChangedEventArgs> m_BulkChangeList = new List<NotifyCollectionChangedEventArgs>();

        [XmlIgnore]
        [Browsable(false)]
        public IProjectContext ProjectContext { get; private set; }

        public new void Add(T item)
        {
            if (Application.Current.Dispatcher.CheckAccess())
            {
                base.Add(item);
            }
            else
            {
                Application.Current.Dispatcher.Invoke(() => base.Add(item));
            }
        }

        public void Add(IEnumerable<T> itemCollection)
        {
            if (Application.Current.Dispatcher.CheckAccess())
            {
                AddInternal(itemCollection);
            }
            else
            {
                Application.Current.Dispatcher.Invoke(() => AddInternal(itemCollection));
            }
        }

        public void Dispose()
        {
            ProjectContext.BulkService.UnRegisterClient(this);
        }

        public void ExecuteBulkReleaseAction()
        {
            try
            {
                foreach (NotifyCollectionChangedEventArgs eventArgs in m_BulkChangeList)
                {
                    base.OnCollectionChanged(eventArgs);
                }
            }
            catch (Exception)
            {
            }
        }

        public new void Remove(T item)
        {
            if (Application.Current.Dispatcher.CheckAccess())
            {
                base.Remove(item);
            }
            else
            {
                Application.Current.Dispatcher.Invoke(() => base.Remove(item));
            }
        }

        public void Remove(IEnumerable<T> itemCollection)
        {
            if (Application.Current.Dispatcher.CheckAccess())
            {
                RemoveInternal(itemCollection);
            }
            else
            {
                Application.Current.Dispatcher.Invoke(() => RemoveInternal(itemCollection));
            }
        }

        public void SetProjectContext(IProjectContext projectContext)
        {
            ProjectContext = projectContext;
            ProjectContext.BulkService.RegisterClient(this);
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if ((ProjectContext != null) && (ProjectContext.BulkService.IsActive))
            {
                m_BulkChangeList.Add(e);
            }
            else
            {
                base.OnCollectionChanged(e);
            }
        }

        private void AddInternal(IEnumerable<T> itemCollection)
        {
            foreach (T item in itemCollection)
            {
                Add(item);
            }
        }

        private void RemoveInternal(IEnumerable<T> itemCollection)
        {
            foreach (T item in itemCollection)
            {
                Remove(item);
            }
        }
    }
}